#!/bin/bash

# Slackware build script for RockboxUtility

# Copyright 2020-2022 Kyle Guinn <elyk03@gmail.com>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=rbutil
SRCNAM=RockboxUtility
VERSION=${VERSION:-1.5.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$(uname -m) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM-$VERSION
OUTPUT=${OUTPUT:-/tmp}

DOCS="docs/COPYING docs/CREDITS utils/rbutilqt/changelog.txt"

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-v$VERSION-src
tar xvf $CWD/$SRCNAM-v$VERSION-src.tar.bz2
cd $SRCNAM-v$VERSION-src
chown -R root:root .
chmod -R u+w,go-w,a+rX-st .

# Use the system quazip.  Thanks to gentoo.
patch -p1 < $CWD/patches/rbutil-1.5.1-system-quazip.patch
patch -p1 < $CWD/patches/rbutil-1.5.1-cmake.patch
patch -p1 < $CWD/patches/rbutil-1.5.1-headers.patch

# Use the system bzip2, only for use with bspatch
# (another 3rd party program, modified by Rockbox
# for use as a library, so cannot be unbundled).
patch -p1 < $CWD/patches/system-bzip2.patch

# Use the system mspack.
patch -p1 < $CWD/patches/system-mspack.patch

# Use the system tomcrypt.
patch -p1 < $CWD/patches/system-tomcrypt.patch

# Ensure we don't build/use these bundled libs.
patch -p1 < $CWD/patches/bundled-licenses.patch
rm -rf \
  lib/rbcodec/codecs/libspeex \
  utils/rbutilqt/mspack \
  utils/rbutilqt/quazip \
  utils/rbutilqt/zlib \
  utils/bzip2 \
  utils/tomcrypt \

# Decrease the amount of log spam.
sed -i 's/Logger::Debug/Logger::Warning/' utils/rbutilqt/logger/src/AbstractAppender.cpp

mkdir -p utils/build
cd utils/build
  cmake \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_RULE_MESSAGES=OFF \
    -DCMAKE_VERBOSE_MAKEFILE=TRUE \
    -DBUILD_SHARED_LIBS=OFF \
    -DCCACHE_PROGRAM=FALSE \
    -DUSE_SYSTEM_QUAZIP=ON \
    ..
  make
  install -D -m 0755 -s rbutilqt/RockboxUtility $PKG/usr/bin/RockboxUtility
cd ../..
install -D -m 0644 utils/rbutilqt/RockboxUtility.desktop $PKG/usr/share/applications/RockboxUtility.desktop
install -D -m 0644 docs/logo/rockbox-clef.svg $PKG/usr/share/icons/hicolor/scalable/apps/rockbox-clef.svg

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
